package cn.jsj.machine.controller;

import cn.jsj.machine.exceptions.MyRuntimeIdentityException;
import cn.jsj.machine.exceptions.MyRuntimeParamsException;
import cn.jsj.machine.pojo.CPart;
import cn.jsj.machine.service.PartService;
import cn.jsj.machine.tool.HashMapBuilder;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ConstraintViolationException;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Validated
@Controller
public class PartController {

    @Autowired
    private PartService partService;

    @Autowired
    private HashMapBuilder hashMapBuilder;

    @ResponseBody
    @RequestMapping(value = "getparts", method = {RequestMethod.POST}, params = {"account","password"})
    public Map<String,Object> getPart(@RequestParam("account") Integer account,@RequestParam("password") String password){
        Map<String,Object> result = null;
        try {
            List<CPart> storage = this.partService.getPartList(account, password.trim());
            if(storage != null && storage.size()>0){
                Collections.sort(storage);
            }
            result = this.hashMapBuilder.buildList(storage);
        }catch (ConstraintViolationException | MyRuntimeIdentityException e){
            result = this.hashMapBuilder.buildList(null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "addOnePart",method = {RequestMethod.POST}, params = {"account","password","partName","remainNumber","limitNumber","rmb"})
    public Map<String,Object> addOnePart(@RequestParam("account") Integer account,@RequestParam("password") String password,@RequestParam("partName") String partName,@RequestParam("remainNumber") Integer remainNumber,@RequestParam("limitNumber") Integer limitNumber,@RequestParam("rmb")@NotNull@Min(value = 0) Double rmb){
        Map<String,Object> result = null;
        try {
            this.partService.addOnePart(account, password, partName, remainNumber, limitNumber,rmb);
            result = this.hashMapBuilder.build(1,"添加成功",null);
        }catch (ConstraintViolationException | MyRuntimeIdentityException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "deleteOnePart", method = {RequestMethod.POST}, params = {"account","password","partID"})
    public Map<String,Object> deleteOnePartInformation(Integer account, String password, Integer partID){
        Map<String,Object> result = null;
        try {
            this.partService.deleteOnePartInformation(account, password, partID);
            result = this.hashMapBuilder.build(1,"删除成功",null);
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "reName", method = {RequestMethod.POST}, params = {"account","password","partID","newName"})
    public Map<String,Object> resetPartName(@RequestParam("account") Integer account,@RequestParam("password") String password,@RequestParam("partID") Integer partID,@RequestParam("newName") String newName){
        Map<String,Object> result = null;
        try {
            this.partService.resetPartName(account,password,partID,newName);
            result = this.hashMapBuilder.build(1,"修改成功",null);
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "cutOnePartNumber", method = {RequestMethod.POST}, params = {"account","password","partID","cutNumber"})
    public Map<String,Object> cutPartNumber(@RequestParam("account") Integer account,@RequestParam("password") String password,@RequestParam("partID") Integer partID,@RequestParam("cutNumber") Integer cutNumber){
        Map<String,Object> result = null;
        try {
            this.partService.cutPartNumber(account, password, partID, cutNumber);
            result = this.hashMapBuilder.build(1,"库存信息修改成功",null);
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "addOnePartNumber", method = {RequestMethod.POST}, params = {"account","password","partID","addNumber"})
    public Map<String,Object> addNumberOfPart(@RequestParam("account") Integer account,@RequestParam("password") String password,@RequestParam("partID") Integer partID,@RequestParam("addNumber") Integer addNumber){
        Map<String,Object> result = null;
        try {
            this.partService.addPartNumber(account, password, partID, addNumber);
            result = this.hashMapBuilder.build(1,"库存信息修改成功",null);
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "resetRmb", method = {RequestMethod.POST}, params = {"account","password","partID","newRmb"})
    public Map<String,Object> resetRmbInformation(@RequestParam("account") Integer account,@RequestParam("password") String password,@RequestParam("partID") Integer partID,@RequestParam("newRmb") Double newRmb){
        Map<String,Object> result = null;
        try {
            this.partService.resetRmbOfPart(account, password, partID, newRmb);
            result = this.hashMapBuilder.build(1,"修改成功",null);
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "reSetWaring", method = {RequestMethod.POST}, params = {"account","password","partID","waringValue"})
    public Map<String,Object> resetWaringValue(@RequestParam("account") Integer account,@RequestParam("password") String password,@RequestParam("partID") Integer partID,@RequestParam("waringValue") Integer waringValue){
        Map<String,Object> result = null;
        try {
            this.partService.resetPartWaringValue(account,password,partID,waringValue);
            result = this.hashMapBuilder.build(1,"修改成功", null);
        }catch (ConstraintViolationException | MyRuntimeIdentityException | MyRuntimeParamsException e){
            result = this.hashMapBuilder.build(0,e.getMessage(),null);
        }catch (Exception e){
            throw e;
        }
        return result;
    }
}
